package cn.rkylin.oms.system.log.dao;

import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.common.dto.PagedData;
import cn.rkylin.oms.system.flag.domain.Flag;
import cn.rkylin.oms.system.log.domain.LogRecord;
import cn.rkylin.oms.system.log.domain.SearchConditions;
import cn.rkylin.oms.system.project.vo.ProjectVO;

@Repository
public class LogDaoImpl implements ILogDao {

	private static final String COLLECTION_NAME = "oms_log";

	@Autowired
	private MongoTemplate mongoTemplate;
    @Autowired
    protected IDataBaseFactory dao;

	@Override
	public PagedData<LogRecord> search(Pageable pageable, SearchConditions conditions) throws Exception {
		Query query = new Query().with(pageable).addCriteria(buildCriteria(conditions));
		long total = mongoTemplate.count(query, COLLECTION_NAME);
		List<LogRecord> records = mongoTemplate.find(query, LogRecord.class, COLLECTION_NAME);
		return new PagedData<>(records, total);
	}

    /**
     * 插入日志
     * 
     * @param logRecord
     */
	@Override
    public int insert(LogRecord logRecord) throws Exception{
        return dao.insert("insertLog", logRecord);
    }
	
	private Criteria buildCriteria(SearchConditions conditions) {
		Criteria criteria;
		if (conditions.isQuickSearch()) {
			criteria = buildSimpleCriteria(conditions);
		} else {
			criteria = buildAdvanceCriteria(conditions);
		}
		return criteria;
	}

	private Criteria buildSimpleCriteria(SearchConditions conditions) {
		Criteria criteria;
		criteria = new Criteria();
		if (isNotBlank(conditions.getKeyword())) {
			criteria = criteria.andOperator(where("logType").is("trace"),
					where("detail").regex(conditions.getKeyword()));
		}
		return criteria;
	}

	private Criteria buildAdvanceCriteria(SearchConditions conditions) {
		List<Criteria> criterias = new ArrayList<>();
		criterias.add(where("logType").is("trace"));
		if (isNotBlank(conditions.getBizId())) {
			criterias.add(where("bizId").is(conditions.getBizId()));
		}
		if (isNotBlank(conditions.getTag())) {
			criterias.add(where("tag").is(conditions.getTag()));
		}
		if (isNotBlank(conditions.getUser())) {
			criterias.add(where("user").is(conditions.getUser()));
		}
		if (isNotBlank(conditions.getOperation())) {
			criterias.add(where("operation").is(conditions.getOperation()));
		}
		if (isNotBlank(conditions.getSourceType())) {
			criterias.add(where("operTime").gte(conditions.getOperStartTime()));
		}
		if (isNotBlank(conditions.getLogType())) {
			criterias.add(where("operTime").lte(conditions.getOperEndTime()));
		}
		return new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()]));
	}
}
